package com.pengtu.web.asset.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.User;
import com.pengtu.entity.app.UserDepartment;
import com.pengtu.entity.asset.Message.Publish;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.asset.MessageManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/asset/message")
@Result(name = BaseActionSupport.RELOAD, location = "publish.action", type = "redirect")
public class PublishAction extends BaseActionSupport<Publish> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MessageManager messageManager;
	private AccountManager accountManager;
	private SystemManager systemManager;

	private String id;
	private Publish publish;
	private List<Department> deptList;
	private Page<Publish> page = new Page<Publish>(10);
	private User user;
	private Department department;
	private Attachment attachmention;

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public User getUser() {
		return user;
	}

	public Department getDepartment() {
		return department;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public Page<Publish> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	@Override
	public Publish getModel() {
		// TODO Auto-generated method stub
		return publish;
	}

	@Action("/asset/message/mana/publish")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		deptList = accountManager.getDepartment();
		page = messageManager.findPagePublish(page, queryFilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		user = SpringSecurityUtils.getCurrentUser();
		if (StringUtils.isEmpty(id) && null != user) {
			Set<UserDepartment> deptArray = user.getUserDepartmentList();
			if (deptArray.size() > 0)
				department = deptArray.iterator().next().getDepartment();
		}
		deptList = accountManager.getDepartment();
		return INPUT;
	}

	@Action(value = "/asset/message/publish!save.action", interceptorRefs = {
			@InterceptorRef(value = "fileUpload", params = {
					"AllowedExtensions", "jpg,gif,doc,txt", "MaximumSize",
					"1048576" }), @InterceptorRef("defaultStack") })
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		/** 添加发布人 **/
		if (StringUtils.isEmpty(id)) {
			publish.setPublishPerson((User) SpringSecurityUtils
					.getCurrentUser());
		} else {
			publish.setPublishPerson(publish.getPublishPerson());
		}
		/** 获得部门ID, 添加部门 **/
		String dept = Struts2Utils.getParameter("deptId");
		publish.setDepartment(accountManager.getDepartment(dept));
		/** 保存公告对象 **/
		messageManager.saveOrupdatePublish(publish);
		addActionMessage("Title为" + publish.getTitle() + "的公告添加成功Q@!");
		/** 判断是否有上传附件，如果有上传附件 **/
		if (null != attachmention) {

			attachmention.setTableId(publish.getId());
			attachmention.setTableField("Publish_attachment");
			attachmention.setTableName("Publish");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			attachmention.setCreateDate(DateUtils.getCurrentDate());
			publish.setAttachment(attachmention);
			/** 修改公告信息，级联添加附件信息到数据库 **/
			messageManager.saveOrupdatePublish(publish);
			String mes = "";
			if(StringUtils.isNotEmpty(id)){
				mes = "修改";
			}else{
				mes = "新增";
			}
			addActionMessage("Title为" + publish.getTitle() + "的公告附件"
					+ attachmention.getUpfileFileName() + mes+"成功Q@!");
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("Id" + id + "的公告被删除Q@!");
		messageManager.deletePublish(id);
		return RELOAD;
	}

	// 设置公告显示属性
	public String publishShowFalse() {

		String publishsId = Struts2Utils.getParameter("publishsId");
		String showTag = Struts2Utils.getParameter("showTag");
		messageManager.updatePublishShow(publishsId, showTag);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			publish = messageManager.getPublish("id", id);
		} else {
			publish = new Publish();
		}
	}

	
	public String view()throws Exception{
		return VIEW;
	}
	/**
	 * 定义在view()前执行二次绑定.
	 */
	public void prepareView() throws Exception {
		prepareModel();
	}
	
	
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}
	
	
	public void noticeFront(){
		List<Publish> noticeList = new ArrayList<Publish>();
		noticeList = messageManager.getTopNotice();
		String root = Struts2Utils.getRootPath();
		String content = "";
		for(int i = 0; i < noticeList.size();i++){
			Publish publishfront = new Publish();
			publishfront = noticeList.get(i);
			content += "<p><a href='"+root+"/asset/message/publish!view.action?id="+publishfront.getId()+"' title='"+publishfront.getCreateDate()+"'>"
			+(i+1)+".&nbsp;"+publishfront.getTitle()+"</a></p>";
		}
		//content += "";
		Struts2Utils.renderText(content);
	}
	

}
